package com.example.service;

import com.example.POJO.Account;
import com.example.POJO.Product;
import com.example.mapper.AccountMapper;
import com.example.mapper.ProductMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class ProductService {
    public List<Product> getProductBySearch(Product product) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession();

        ProductMapper productMapper=sqlSession.getMapper(ProductMapper.class);
        List<Product> products=productMapper.getProductBySearch(product);
        sqlSession.close();
        return products;
    }

    public Product getProductByID(Integer productId) throws IOException {
        String resource= "mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession();

        ProductMapper productMapper=sqlSession.getMapper(ProductMapper.class);
        Product product=productMapper.getProductById(productId);
        sqlSession.close();
        return product;
    }
}
